from django.db import models
from utils.common_model import BaseModel
from mptt.models import MPTTModel


class Menu(BaseModel, MPTTModel):
    """
    上级菜单id, 子菜单数目， 菜单类型， 菜单标题， 组件名称， 组件， 排序， 图标， 连接地址， 是否外链， 缓存， 隐藏
    权限
    """
    parent = models.ForeignKey('self', verbose_name='父菜单id', related_name='menus', on_delete=models.SET_NULL,
                               null=True, blank=True)
    sub_count = models.IntegerField(verbose_name='子菜单数目', null=True, blank=True)
    type_choice = (
        (0, '主菜单'),
        (1, '子菜单'),
        (2, '按钮')
    )
    permission = models.CharField(max_length=64, verbose_name='权限内容',
                                  help_text='负责存储按钮的权限，保存格式为【菜单:动作】',
                                  null=True, blank=True)
    type = models.IntegerField(choices=type_choice, verbose_name='菜单类型', blank=True, null=True)
    title = models.CharField(max_length=32, verbose_name='菜单标题', blank=True, null=True, unique=True)
    name = models.CharField(max_length=255, verbose_name='前端组件名称', blank=True, null=True, unique=True)
    component = models.CharField(max_length=255, verbose_name='前端组件', blank=True, null=True)
    menu_sort = models.IntegerField(verbose_name='菜单排序', blank=True, null=True)
    icon = models.FileField(upload_to='menu', default='menu/default.png')
    path = models.CharField(max_length=255, blank=True, null=True, verbose_name='菜单链接地址')
    is_frame = models.BooleanField(default=False, verbose_name='是否外链')

    class Meta:
        db_table = 'ccsp_backend_menu'


class BackendLog(BaseModel):
    '''
    继承basemodel获得创建时间、更新时间、是否删除、是否展示字段
    '''
    user = models.CharField(max_length=64, verbose_name='用户', help_text='哪个用户执行的操作', default='AnonymousUser')
    user_agent = models.TextField(verbose_name='操作用户的ua', help_text='可以用来统计用户所使用的平台')
    view = models.CharField(max_length=128, verbose_name='操作的视图', null=True, blank=True)
    view_args = models.TextField(verbose_name='视图参数', null=True, blank=True)
    view_kwargs = models.TextField(verbose_name='视图参数', null=True, blank=True)
    method = models.CharField(max_length=128, verbose_name='请求方法', null=True, blank=True)
    path = models.CharField(max_length=256, verbose_name='访问的路径', null=True, blank=True)
    addr = models.CharField(max_length=256, verbose_name='访问的ip', null=True, blank=True)
    parameter = models.TextField(verbose_name='参数', null=True, blank=True)
    expend_time = models.CharField(max_length=128, verbose_name='总计时间')
    status = models.CharField(max_length=64, verbose_name='响应状态码', default=100)

    class Meta:
        db_table = 'ccsp_backend_log'
